package cn.q3c.web.tool.dao;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import org.apache.commons.lang3.ArrayUtils;

public class BaseDAO {
	
	private static Logger logger = Logger.getLogger(BaseDAO.class);

	// 数据源
	@Autowired
	@Qualifier("dataSource")
	protected ComboPooledDataSource dataSource;

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(ComboPooledDataSource dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * 得到该链接刚刚自动生成的ID
	 * 
	 * @param conn
	 * @return
	 */
	public long getLastInsertID(Connection conn) {
		String sql = "SELECT LAST_INSERT_ID() as nid";

		try {

			Object retObj = this.query(conn, sql, new MapHandler()).get("nid");
			if( retObj instanceof Long)
			{
				return (Long)retObj;
			}
			else if( retObj instanceof BigInteger)
			{
				BigInteger b = (BigInteger)retObj;
				return b.intValue();
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return -1;
	}
	
	
	protected Object query( Connection conn, String sql, ResultSetHandler<?> rsh, Object... params ) throws SQLException
	{
		QueryRunner qr = new QueryRunner();
		
		logger.debug("sql:" + sql);
		
		Object obj = qr.query(conn, sql, rsh, params);
		
		String logstr = "sql:" + sql + ";params:" + ArrayUtils.toString(params) + ";";
		logger.debug(logstr);
		
		
		printDBPool();
		return obj;
	}
	
	
	protected Map<String,Object> query( Connection conn, String sql, ResultSetHandler<Map<String,Object>> rsh ) throws SQLException
	{
		QueryRunner qr = new QueryRunner();
		
		logger.debug("sql:" + sql);
		
		Map<String,Object> obj = qr.query(conn, sql, rsh);
		String logstr = "sql:" + sql + ";";
		logger.debug(logstr);
		
		printDBPool();
		return obj;
	}
	
	protected void update( Connection conn, String sql, Object... params ) throws SQLException
	{
		QueryRunner qr = new QueryRunner();
		
		logger.debug("sql:" + sql);
		
		qr.update(conn, sql, params);
		String logstr = "sql:" + sql + ";params:" + ArrayUtils.toString(params) + ";";
		logger.debug(logstr);
		
		printDBPool();
	}	
	
	private void printDBPool() throws SQLException
	{
		logger.debug("ThreadPoolSize=" + this.dataSource.getThreadPoolSize());
		logger.debug("ThreadPoolNumActiveThreads=" + this.dataSource.getThreadPoolNumActiveThreads());
		logger.debug("ThreadPoolNumIdleThreads=" + this.dataSource.getThreadPoolNumIdleThreads());
		logger.debug("ThreadPoolNumTasksPending=" + this.dataSource.getThreadPoolNumTasksPending());
	}
}
